<template>
	<view>
		<view class="content">
			<view class="score">
				分数：{{score}}
			</view>
			<view class="time">
				用时：{{minute < 10 ? '0' + minute : minute}}:{{second < 10 ? '0' + second : second}}:{{second100 < 10 ? '0' + second100 : second100}}
			</view>
			<view class="blockList" :animation="listAnimation" :style="{bottom : listBottom}">
				<view class="blockRow" v-for="(items, index1) in blockList" :key="index1">
					<view :animation="clickAnimationNum + index1 + index2" class="blockItem" :style="{background: item.color}" v-for="(item, index2) in items"
					 :key="index2" @click="tread(index1, index2)"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				blockList: [],
				listBottom: '0px',
				score: 0,
				minute: 0,
				second: 0,
				second100: 0,
				timer: 0,
				isGameing: 0,
				listAnimation: '',
				speed: 1,
				windowHeight: 0,
				min: 0,
				max: 8,
				speedTimer: '',
				addListTimer: '',
				clickAnimation: {},
				cleanAnimation: {},
				clickAnimationNum: ''
			}
		},
		onLoad() {
			this.init()
		},
		onBackPress() {
			clearInterval(this.timer);
			clearInterval(this.addListTimer);
			clearInterval(this.speedTimer);	
		},
		methods: {
			/* 页面初始化 */
			init() {
				var that = this;
				this.second100 = 0;
				this.second = 0;
				this.minute = 0;
				this.score = 0;
				this.isGameing = 0;
				this.max = 8;
				this.min = 0;
				this.speed = 1;
				this.listBottom = '0px';
				this.animation = uni.createAnimation({
					duration: 100,
					timeingFunction: 'esay',
				})
				this.animation.bottom(0).step();
				this.listAnimation = this.animation.export()
				clearInterval(this.timer);
				clearInterval(this.addListTimer);
				clearInterval(this.speedTimer);
				this.blockList = [];
				for (var i = 0; i <= this.max; i++) {
					this.blockList.push(this.getRow())
				}
				/* 获取屏幕高度 */
				uni.getSystemInfo({
					success(res) {
						that.windowHeight = res.windowHeight;
					}
				})

				/* 配置下滑动画 */
				this.animation = uni.createAnimation({
					duration: 1000000,
					timingFunction: 'linear',
				})
				this.clickAnimation = uni.createAnimation({
					duration: 100,
					timeingFunction: 'esay-in-out',
					transformOrigin: '0 100% 0'
				})

				uni.showModal({
					title: '滑块速度会越来越快哦，点击开始游戏吧',
					confirmText: '开始游戏',
					success(res) {
						if(res.confirm){
							setTimeout(() => {
								that.start()
							}, 1000)
						}
					}
				})
			},
			/* 点击触发踩事件 */
			tread(index1, index2) {
				if (this.blockList[index1][index2].color == '#000000') {
					this.blockList[index1][index2].color = '#ffffff'
					this.score++;
				} else {
					this.end()
				}
			},

			/* 开始计时 */
			start() {
				this.isGameing = 1;
				this.startAnimation()
				/* 定时往末尾添加行 */
				this.addRowTimer()
				/* 秒表定时器 */
				this.timer = setInterval(() => {
					if (this.second == 59) {
						this.minute++;
						this.second = 0;
					} else if (this.second100 == 99) {
						this.second++;
						this.second100 = 0;
					} else {
						this.second100++;
					}
				}, 10)

				/* 加速定时器 */
				this.speedTimer = setInterval(() => {
					this.speed += 0.5;
					this.animation.translateY(this.speed * this.windowHeight / 5 * 1000).step();
					this.listAnimation = this.animation.export()
					clearInterval(this.addListTimer);
					this.addRowTimer()
				}, 10000)

			},
			/* 开始下滑动画 */
			startAnimation() {
				this.animation.translateY(this.speed * this.windowHeight / 5 * 1000).step();
				this.listAnimation = this.animation.export()
			},
			/* 游戏结束 */
			end() {
				var that = this;
				let time = (this.minute < 10 ? '0' + this.minute : this.minute) + ':' + (this.second < 10 ? '0' + this.second :
					this.second) + ':' + (this.second100 < 10 ? '0' + this.second100 : this.second100)
				if (this.isGameing == 1) {
					this.isGameing = 0;
					uni.showModal({
						title: '用时：' + time + '，得分：' + this.score,
						showCancel: false,
						confirmText: '重新开始',
						success(res) {
							setTimeout(() => {
								that.init()
							}, 500)
						}
					})
					
				}

			},
			/* 添加新行定时器 */
			addRowTimer() {
				this.addListTimer = setInterval(() => {
					this.max++;
					this.min++;
					console.log(this.min);
					this.blockList[this.max] = this.getRow()
					// delete this.blockList[this.min]
					
					for (var i = 0; i < 4; i++) {
						if (this.blockList[this.min][i].color == '#000000') {
							this.end()
						}
					}
				}, 1000 / this.speed)
			},
			/* 获取随机数 */
			random(min, max, isInt) {
				var result = false
				result = min + Math.random() * (max - min + 1)
				if (isInt) {
					result = parseInt(result)
				}
				return result
			},
			/* 获取新的一行 */
			getRow() {
				let data = [{
						color: '#ffffff',
					},
					{
						color: '#ffffff',
					},
					{
						color: '#ffffff',
					},
					{
						color: '#ffffff',
					}
				]
				data[this.random(0, 3, true)].color = '#000000'
				return data;
			}
		},

	}
</script>

<style lang="less">
	.score {
		position: fixed;
		color: #a0a0a0;
		height: 20px;
		left: 20upx;
		z-index: 999;
	}

	.time {
		position: fixed;
		color: #a0a0a0;
		height: 20px;
		right: 20upx;
		z-index: 999;
	}

	.content {
		/* #ifdef H5 */
		height: calc((100vh - 44px));
		/* #endif */
		/* #ifdef MP-WEIXIN */
		height: 100vh;
		/* #endif */
	}

	.blockList {
		display: flex;
		flex-wrap: wrap;
		flex-direction: column-reverse;
		position: fixed;
		bottom: 0;

		.blockRow {
			display: flex;

			.blockItem {
				width: 25vw;
				box-sizing: border-box;
				/* #ifdef H5 */
				height: calc((100vh - 44px) / 5);
				/* #endif */
				/* #ifdef MP-WEIXIN */
				height: 20vh;
				/* #endif */
				border: 1upx solid #000;
			}
		}

	}
</style>
